﻿/**
 * DynaInterpolator
 * 
 * @author		Allen Chou
 * @version		1.0.0 (last update: Apr 7 2009)
 * @link        http://cjcat.blogspot.com
 * @link		http://cjcat2266.deviantart.com
 */

package idv.cjcat.utils {
	import flash.display.*;
	
	/**
	 * Similar to the <code>DynaShape</code> package, the numeric data in a <code>MassInterpolator</code> object is no more static.
	 * <p>
	 * Instead, you pass in a series of objects with defined properties <code>x</code> and <code>y</code>.
	 * Each time the <code>update()</code> method is called, their two-dimensional vector data are read into the interpolator, ready for subsequent interpolation.
	 * </p>
	 */
	public class DynaInterpolator {
		
		/** @private */
		protected var _objs:Array;
		/** @private */
		protected var _mi:MassInterpolator = new MassInterpolator(2);
		
		public function DynaInterpolator(objs:Array = null) {
			if (objs != null) _objs = objs;
			else _objs = new Array();
		}
		
		public function update():void {
			_mi.clear();
			for each (var obj:* in _objs) {
				_mi.push(obj.x, obj.y);
			}
		}
		
		public function interpolate(t:Number, usePercentage:Boolean = false):Object {
			if (_objs.length == 0) return null;
			
			var v2:Vector.<Number> = _mi.interpolate(t, usePercentage);
			return {x:v2[0], y:v2[1]};
		}
		
		public function get length():Number {
			return _mi.length;
		}
		
		public function push(obj:*):void {
			_objs.push(obj);
		}
		
		public function setObjs(objs:*):void {
			_objs = objs.concat();
		}
		
		public function insert(index:int, obj:*):void {
			_objs.splice(index, 0, obj);
		}
		
		public function clear():Array {
			var temp:Array = _objs;
			_objs = new Array();
			return temp;
		}
	}
}